package com.example.cookbook.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.cookbook.R;
import com.example.cookbook.adapter.MyFragmentAdapter;
import com.example.cookbook.fragment.OneFragment;
import com.example.cookbook.fragment.ThreeFragment;
import com.example.cookbook.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;
//导航主界面
public class MainActivity extends AppCompatActivity  {
    //定义
    private ViewPager view_pager;
    private RadioButton radio_one;      //主页按钮
    private RadioButton radio_two;      //收藏按钮
    private RadioButton radio_three;    //个人中心按钮
    private RadioGroup radio_grope;     //单选按钮


    private Fragment onefragment,twofragment,threefragment;     //三个导航
    private List<Fragment> fragarrlist=new ArrayList<>();       //动态数组，存放数据
    private int position=0;                                     //默认选中第一个界面

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定控件
        initView();
        //数据初始化
        initData();
        //ViewPager滑动时选中对应的导航栏按钮
        initMove();
        //底部导航栏按钮单击事件
        initOnClick();
    }

    //绑定控件
    private void initView() {
        view_pager= findViewById(R.id.view_pager);
        radio_one= findViewById(R.id.radio_one);
        radio_two= findViewById(R.id.radio_two);
        radio_three= findViewById(R.id.radio_three);
        radio_grope= findViewById(R.id.radio_group);
    }

    //数据初始化
    private void initData() {
        //将三个页面实例化，存放在动态数组中
        onefragment=new OneFragment();
        twofragment=new TwoFragment();
        threefragment=new ThreeFragment();
        fragarrlist.add(onefragment);
        fragarrlist.add(twofragment);
        fragarrlist.add(threefragment);
        //自定义适配器
        MyFragmentAdapter adapter = new MyFragmentAdapter(this.getSupportFragmentManager(), fragarrlist);
        //绑定适配器
        view_pager.setAdapter(adapter);
        //将RadioGroup中特定索引位置的RadioButton设置为选中状态
        ((RadioButton)radio_grope.getChildAt(position)).setChecked(true);
    }

    //ViewPager滑动时选中对应的导航栏按钮
    private void initMove() {       //registerOnPageChangeCallback方法来添加一个页面更改的回调监听器
        view_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {      //添加一个页面更改的回调监听器
                ((RadioButton)radio_grope.getChildAt(position)).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    //底部导航栏按钮单击事件
    private void initOnClick() {
        radio_grope.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.radio_one) {
                position = 0;
            } else if (checkedId == R.id.radio_two) {
                position = 1;
            } else if (checkedId == R.id.radio_three) {
                position = 2;
            }
            view_pager.setCurrentItem(position);
        });
    }

}